package com.windy.common.transmit;


import cn.hutool.core.collection.IterUtil;
import com.windy.common.header.Headers;
import com.windy.common.header.HttpHeader;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.function.Supplier;

/**
 * Created by liyufeng on 2022/5/8
 */
public class WebTransmitHeader extends TransmitHeader<HttpServletRequest> {

    /**
     * 从配置中获取
     */
    private List<String> headerNames;

    public void setHeaderNames(List<String> headerNames) {
        this.headerNames = headerNames;
    }

    /**
     * 入口一般不需要重放
     * @param headers
     */
    @Override
    protected void replayHeader( Headers headers,Supplier<HttpServletRequest>supplier) {

    }

    @Override
    protected Headers pickerHeader(Supplier<HttpServletRequest>requestSupplier) {
        Headers headers = new Headers();
        IterUtil.asIterator(requestSupplier.get().getHeaderNames())
                .forEachRemaining(key->{
                    if(headerNames.contains(key)){
                        headers.add(new HttpHeader(key,requestSupplier.get().getHeader(key)));
                    }
                });
        return headers;
    }
}
